package ${servicePackage}.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qicai.common.exception.ServiceDataException;
import com.qicai.common.page.Page;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.BeanUtils;
import ${daoPackage}.${className}Mapper;
import ${servicePackage}.${className}Service;
import ${packageName}.${className};
import ${topLevel}.dto.${className}DTO;
import java.util.ArrayList;
import java.util.List;
/**
 * ${tablecomment}业务层访问接口实现
 * @author  ${author}
 * @version ${classObjectName}ServiceImpl.java, v 0.1  ${date?datetime}
 */
@Service("${classObjectName}Service")
public class ${className}ServiceImpl implements ${className}Service {

private static final org.slf4j.Logger logger = LoggerFactory.getLogger(${className}ServiceImpl.class);
    /**${tablecomment}数据层访问接口实现**/
	@Autowired
	private ${className}Mapper ${classObjectName}Mapper;

    @Override
       public Long save${className}(${className}DTO ${classObjectName}DTO) {
           logger.info("save${className} ${classObjectName}DTO:{}", ${classObjectName}DTO);
                  long rows = 0;
                  try {
                      if (${classObjectName}DTO != null) {
                          ${className} ${classObjectName} = new ${className}();
                          BeanUtils.copyProperties(${classObjectName}DTO, ${classObjectName});
                          if (${classObjectName}.getId() != null && ${classObjectName}.getId() != 0) {
                              //更新
                              ${classObjectName}.setUpdateTime(System.currentTimeMillis());
                              rows = ${classObjectName}Mapper.updateByPrimaryKeySelective(${classObjectName});
                          } else {
                              //插入
                              ${classObjectName}.setCreateTime(System.currentTimeMillis());
                              rows = ${classObjectName}Mapper.insert(${classObjectName});
                          }
                      }
                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                      throw new ServiceDataException("save${className} Error::", e);
                  }
                  return rows;
       }

       @Override
       public Long delete${className}(Long id) {
       logger.info("delete${className} ${classObjectName}Id:{}", id);
           long rows = 0;
           try {
               rows = ${classObjectName}Mapper.deleteByPrimaryKey(id);
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("delete${classObjectName} Error::", e);
           }
           return rows;
       }

       @Override
       public ${className}DTO query${className}ById(Long id) {
       logger.info("query${className}ById ${classObjectName}Id:{}", id);
           ${className}DTO ${classObjectName}DTO = null;
           try {
              ${className} ${classObjectName} = ${classObjectName}Mapper.selectByPrimaryKey(id);
                           if(${classObjectName}!=null){
                           ${classObjectName}DTO = new ${className}DTO();
                               BeanUtils.copyProperties(${classObjectName},${classObjectName}DTO);
                           }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("query${classObjectName}ById", e);
           }
           return ${classObjectName}DTO;
       }

       @Override
       public List<${className}DTO> query${className}(${className}DTO ${classObjectName}DTO) {
            logger.info("query${className} ${classObjectName}DTO:{}", ${classObjectName}DTO);
                  List<${className}DTO> list = null;
                  List<${className}> ${classObjectName}List = null;
                  try {
                      if (${classObjectName}DTO != null) {
                          ${className} ${classObjectName} = new ${className}();
                          BeanUtils.copyProperties(${classObjectName}DTO, ${classObjectName});
                          ${classObjectName}List = ${classObjectName}Mapper.select(${classObjectName});
                      } else {
                          Example example = new Example(${className}.class);
                          // 排序规则
                          example.orderBy("id").desc();
                          ${classObjectName}List = ${classObjectName}Mapper.selectByExample(example);
                      }
                      if (${classObjectName}List != null && ${classObjectName}List.size() > 0) {
                          list = convert${className}DTO(${classObjectName}List);
                      }

                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                  }
                  return list;
       }

       @Override
       public Page<${className}DTO> query${className}Page(${className}DTO ${classObjectName}DTO) {
        logger.info("query${className}Page ${classObjectName}DTO:{}", ${classObjectName}DTO);
           Page<${className}DTO> page = null;
           Integer currentPage = ${classObjectName}DTO.getPage();
           if (currentPage < 1) currentPage = 1;
           try {
               page = new Page<${className}DTO>(currentPage, ${classObjectName}DTO.getPageCount());
               PageHelper.startPage(currentPage, ${classObjectName}DTO.getPageCount());
               Example example = new Example(${className}.class);
               // 排序规则
               example.orderBy("id").desc();
               List<${className}> list = ${classObjectName}Mapper.selectByExample(example);

               if (list != null && list.size() > 0) {
                   PageInfo pageInfo = new PageInfo(list);
                   page.setTotalCount(Long.valueOf(pageInfo.getTotal()).intValue());
                    List<${className}DTO> ${classObjectName}DTOList = convert${className}DTO(list);
                   page.setResult(${classObjectName}DTOList);
               }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
           }

           return page;
       }
	  private List<${className}DTO> convert${className}DTO(List<${className}> ${classObjectName}List) {
            
            if (${classObjectName}List == null) return null;
             List<${className}DTO> ${classObjectName}DTOs = new ArrayList<>();
             for (${className} ${classObjectName} : ${classObjectName}List
                     ) {
                 ${className}DTO ${classObjectName}DTO = new ${className}DTO();
                 BeanUtils.copyProperties(${classObjectName}, ${classObjectName}DTO);
                 ${classObjectName}DTOs.add(${classObjectName}DTO);
             }
             return ${classObjectName}DTOs;
         }
}